package cn.schoolwow.util.test.module.query.instanceList.flow.group;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.test.module.common.domain.User;
import org.junit.Assert;

import java.util.List;
import java.util.Map;

public class InstanceQueryGroupByFieldFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        List<User> userList = (List<User>) flowContext.checkData("userList");
        String password = (String) flowContext.checkData("password");
        List<String> roleNameList = (List<String>) flowContext.checkData("roleNameList");

        Map<String,List<User>> roleNameUserListMap = QueryInstanceList.newQuery(userList)
                .addQuery("password", password)
                .execute()
                .groupByFieldName("roleName", String.class);
        Assert.assertEquals(roleNameList.size(), roleNameUserListMap.size());
        for(String roleName:roleNameUserListMap.keySet()){
            List<User> singleRoleNameUserList = roleNameUserListMap.get(roleName);
            for(User user:singleRoleNameUserList){
                Assert.assertEquals(roleName, user.roleName);
            }
            Assert.assertEquals(userList.size()/2, singleRoleNameUserList.size());
        }
    }

    @Override
    public String name() {
        return "根据指定字段分组";
    }
}
